﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Controler;
using Waiter.Common;

namespace Waiter.View
{
    public partial class FrmOrderChange : DevComponents.DotNetBar.Office2007Form
    {
        public FrmOrderChange()
        {
            InitializeComponent();
        }

        public FrmOrderChange(string tableCode)
        {
            InitializeComponent();
            this.tableCode = tableCode;
        }
        private string tableCode = null;
        private bool loadingFlag = CommonConstants.FLAG_ON;
        bool dataChangedFlag = CommonConstants.FLAG_OFF;

        public bool DataChangedFlag
        {
            get { return dataChangedFlag; }
            set { dataChangedFlag = value; }
        }
        List<string> tableList = new List<string>();

        private void FrmOrderChange_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderChange.FrmOrderChange_Load", "", "");
            this.loadingFlag = CommonConstants.FLAG_ON;
            fsLoadCmbTable();
            CommonCtrl.fsCmbSetSelectedIndex(this.cmbTable, 0);
            fsLoadDgrMenuList(this.dgrLeft, 1, this.tableCode);
            this.loadingFlag = CommonConstants.FLAG_OFF;
            fsLoadDgrMenuList(this.dgrRight, 2, CommonCtrl.fsCmbGetSelectedValue(this.cmbTable));
        }

        private void fsLoadCmbTable()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderChange.fsLoadCmbTable", "", "");
            TableCtrl tableCtrl = new TableCtrl();
            try
            {
                List<TABLE> tableList = tableCtrl.fsGetTableListForDisplay(null);
                this.cmbTable.Items.Clear();
                if (tableList == null)
                {
                    return;
                }

                List<ComboboxItem> listItem = new List<ComboboxItem>();
                for (int i = 0; i < tableList.Count; i++)
                {
                    if (tableList[i].CODE == this.tableCode)
                    {
                        continue;
                    }
                    ComboboxItem item = new ComboboxItem();
                    item.Text = "Bàn " + tableList[i].TABLE_NAME;
                    item.Value = tableList[i].CODE;
                    listItem.Add(item);
                }
                this.cmbTable.DataSource = listItem;
                this.cmbTable.DisplayMember = CommonConstants.CMB_TEXT;
                this.cmbTable.ValueMember = CommonConstants.CMB_VALUE;
                CommonCtrl.fsCmbSetSelectedIndex(this.cmbTable, 0);
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderChange.fsLoadCmbTable", "", ex.ToString());
            }
        }

        private void fsLoadDgrMenuList(DataGridView dgr, int type, string table)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderChange.fsLoadDgrMenuList", "", "");
            try
            {
                dgr.Rows.Clear();
                OrderCouponCtrl orderCouponCtrl = new OrderCouponCtrl();
                List<ORDER_COUPON_D> orderCouponList = orderCouponCtrl.fsGetCurrentOrderCouponList(table);
                if (orderCouponList == null || orderCouponList.Count == 0)
                {
                    return;
                }

                dgr.Rows.Add(orderCouponList.Count);
                for (int i = 0; i < orderCouponList.Count; i++)
                {
                    dgr.Rows[i].Cells["colName" + type].Value = orderCouponList[i].MENU.DRINKS_NAME;
                    dgr.Rows[i].Cells["colNum" + type].Value = orderCouponList[i].COUNT;
                    dgr.Rows[i].Cells["colCode" + type].Value = orderCouponList[i].MENU_CODE;
                }
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderChange.fsLoadDgrMenuList", "", ex.ToString());
            }
        }

        private void cmbTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderChange.cmbTable_SelectedIndexChanged", "", "");
            if (this.loadingFlag == CommonConstants.FLAG_ON)
            {
                return;
            }

            fsLoadDgrMenuList(this.dgrRight, 2, CommonCtrl.fsCmbGetSelectedValue(this.cmbTable));
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderChange.btnCancel_Click", "", "");
            this.Close();
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderChange.btnLeft_Click", "", "");
            fsAddDgrMenu(this.dgrRight, CommonCtrl.fsCmbGetSelectedValue(this.cmbTable), this.dgrLeft, this.tableCode);
        }

        private void fsAddDgrMenu(DataGridView dgr1, string table1, DataGridView dgr2, string table2)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderChange.fsAddDgrMenu", "", "");
            if (dgr1.Rows.Count == 0)
            {
                return;
            }

            // get dgr2 selected row
            string menuCode = dgr1.CurrentRow.Cells[0].Value.ToString();
            string menuName = dgr1.CurrentRow.Cells[1].Value.ToString();

            // add dgr1
            int idx = -1;
            int count = 0;
            for (int i = 0; i < dgr2.Rows.Count; i++)
            {
                if (dgr2.Rows[i].Cells[0].Value.ToString() == menuCode)
                {
                    count = Convert.ToInt32(dgr2.Rows[i].Cells[2].Value);
                    idx = i;
                    break;
                }
            }

            if (idx == -1)
            {
                dgr2.Rows.Add(1);
                idx = dgr2.Rows.Count - 1;
                dgr2.Rows[idx].Cells[0].Value = menuCode;
                dgr2.Rows[idx].Cells[1].Value = menuName;
            }

            dgr2.Rows[idx].Cells[2].Value = count + 1;
            CommonCtrl.fsDgrSetSelectedRow(dgr2, idx, 1);
            // update DB
            OrderCouponCtrl orderCouponCtrl = new OrderCouponCtrl();
            orderCouponCtrl.fsInsertOrUpdateOrderCoupon(table2, menuCode, 1);

            // update dgr2
            count = Convert.ToInt32(dgr1.CurrentRow.Cells[2].Value);
            if (count < 2)
            {
                dgr1.Rows.RemoveAt(dgr1.CurrentRow.Index);
                orderCouponCtrl.fsRemoveOrUpdateOrderCoupon(table1, menuCode);
            }
            else
            {
                dgr1.CurrentRow.Cells[2].Value = count - 1;
                orderCouponCtrl.fsInsertOrUpdateOrderCoupon(table1, menuCode, -1);
            }

            this.dataChangedFlag = CommonConstants.FLAG_ON;
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderChange.btnRight_Click", "", "");
            fsAddDgrMenu(this.dgrLeft, this.tableCode, this.dgrRight, CommonCtrl.fsCmbGetSelectedValue(this.cmbTable));
        }

        private void btnRightAll_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderChange.btnRightAll_Click", "", "");
            if (this.dgrLeft.Rows.Count == 0)
            {
                return;
            }

            while (this.dgrLeft.Rows.Count > 0)
            {
                fsAddDgrMenu(this.dgrLeft, this.tableCode, this.dgrRight, CommonCtrl.fsCmbGetSelectedValue(this.cmbTable));
            }
            //OrderCouponCtrl orderCouponCtrl = new OrderCouponCtrl();
            //orderCouponCtrl.fsChangeAllOrderCoupon(this.tableCode, CommonCtrl.fsCmbGetSelectedValue(this.cmbTable));
        }

        private void btnLeftAll_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderChange.btnLeftAll_Click", "", "");
            if (this.dgrRight.Rows.Count == 0)
            {
                return;
            }
            while (this.dgrRight.Rows.Count > 0)
            {
                fsAddDgrMenu(this.dgrRight, CommonCtrl.fsCmbGetSelectedValue(this.cmbTable), this.dgrLeft, this.tableCode);
            }
            //OrderCouponCtrl orderCouponCtrl = new OrderCouponCtrl();
            //orderCouponCtrl.fsChangeAllOrderCoupon(CommonCtrl.fsCmbGetSelectedValue(this.cmbTable), this.tableCode);
        }

        private void dgrLeft_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderChange.dgrLeft_CellDoubleClick", "", "");
            this.btnRight_Click(null, null);
        }

        private void dgrRight_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderChange.dgrRight_CellDoubleClick", "", "");
            this.btnLeft_Click(null, null);
        }
    }
}